package dao.Impl;

import dao.TgoodsDao;
import pojo.Tgoods;
import util.BaseDao;

import java.util.ArrayList;
import java.util.List;

public class TgoodsDaoImpl extends BaseDao implements TgoodsDao {
    @Override
    public List<Tgoods> all(int id, String name, int dqys, int myts) {
        String sql = "select * from t_goods where is_delete=0 ";
        ArrayList<Object> list = new ArrayList<>();
        if (id != 0) {
            sql += " and classify_level1_id = ? or classify_level2_id = ? or classify_level3_id = ? ";
            list.add(id);
            list.add(id);
            list.add(id);
        }
        if (name != null && !"".equals(name)) {
            sql += " and goods_name like ? ";
            list.add("%" + name + "%");
        }
        sql += " limit ?,?";
        list.add((dqys - 1) * myts);
        list.add(myts);
        rs = exeQ(sql, list.toArray());
        List<Tgoods> tgoods = new ArrayList<>();
        Tgoods goods = null;
        try {
            while (rs.next()) {
                goods = new Tgoods(rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getDouble(4),
                        rs.getInt(5),
                        rs.getInt(6),
                        rs.getInt(7),
                        rs.getInt(8),
                        rs.getString(9),
                        rs.getInt(10));
                tgoods.add(goods);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return tgoods;
    }

    @Override
    public int alls(int id, String name) {
        int shu = 0;
        String sql = "select count(1) from t_goods where is_delete=0 ";
        ArrayList<Object> list = new ArrayList<>();
        if (id != 0) {
            sql += " and classify_level1_id = ? or classify_level2_id = ? or classify_level3_id = ? ";
            list.add(id);
            list.add(id);
            list.add(id);
        }
        if (name != null && !"".equals(name)) {
            sql += " and goods_name like ? ";
            list.add("%" + name + "%");
        }
        rs = exeQ(sql, list.toArray());

        try {
            if (rs.next()) shu = rs.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return shu;
    }

    @Override
    public Tgoods selById(String id) {
        String sql = "select * from t_goods where id = ? and is_delete=0";
        rs = exeQ(sql, id);
        Tgoods goods = null;
        try {
            while (rs.next()) {
                goods = new Tgoods(rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getDouble(4),
                        rs.getInt(5),
                        rs.getInt(6),
                        rs.getInt(7),
                        rs.getInt(8),
                        rs.getString(9),
                        rs.getInt(10));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return goods;
    }

    @Override
    public void updnum(int num, int id) {
        String sql = "update t_goods set stock = stock -? where id=?";
        exeU(sql, num, id);
    }

    @Override
    public List<Tgoods> getall() {
        String sql = "select * from t_goods where is_delete=0";
        rs = exeQ(sql);
        List<Tgoods> tgoods = new ArrayList<>();
        Tgoods goods = null;
        try {
            while (rs.next()) {
                goods = new Tgoods(rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getDouble(4),
                        rs.getInt(5),
                        rs.getInt(6),
                        rs.getInt(7),
                        rs.getInt(8),
                        rs.getString(9),
                        rs.getInt(10));
                tgoods.add(goods);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return tgoods;
    }

    @Override
    public int delById(int id) {
        String sql = "update t_goods set is_delete=1 where id = ?";
        return exeU(sql, id);

    }

    @Override
    public int addGoods(Tgoods tg) {
        String sql = "insert into t_goods (goods_name, goods_desc, price, stock, classify_level1_id, classify_level2_id, classify_level3_id,\n" +
                "                     file_name,is_delete)\n" +
                "values (?,?,?,?,?,?,?,?,0)";
        return exeU(sql,tg.getGoodsName(),tg.getGoodsDesc(),tg.getPrice(),tg.getStock(),
                tg.getClassifyLevel1Id(),tg.getClassifyLevel2Id(),tg.getClassifyLevel3Id(),
                tg.getFileName());
    }
}
